package com.dxy.md.core;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
@Order(0) // 确保在事务注解前执行
public class DataSourceAspect {

    @Before("@annotation(ds)")
    public void before(JoinPoint point, DataSource ds) {
        log.info("Use DataSource : {} > {}", ds.value(), point.getSignature());
        DataSourceContextHolder.setDataSourceKey(ds.value());
    }

    @After("@annotation(ds)")
    public void after(DataSource ds) {
        log.info("Clear DataSource : {}", ds.value());
        DataSourceContextHolder.clearDataSourceKey();
    }
}
